package com.lh.service.impl;

import com.lh.dao.FileDao;
import com.lh.model.File;
import com.lh.service.FileService;
import com.lh.util.MyException;
import com.lh.util.MyLog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author LuoHang
 * @Date 2022-05-10 23:03
 **/
@Service
public class FileServiceImpl implements FileService {
    @Resource
    private FileDao fileDao;

    @Override
    public boolean upload(File file) {
        return fileDao.upload(file) > 0;
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public File getFileById(Integer id) {
        return fileDao.getFileById(id);
    }

    @Transactional(rollbackFor = MyException.class,propagation = Propagation.REQUIRED,timeout = 5000)
    @Override
    public boolean addFile(File file) throws MyException{
        MyLog.info(FileServiceImpl.class,"事务测试：开始执行新增文件对象.......");
        if (fileDao.upload(file) > 0){
            MyLog.info(FileServiceImpl.class,"事务测试：文件对象添加成功");
            throw new MyException("测试事务FileServiceImpl.addFile()抛出的异常");
        }
        return false;
    }

    @Override
    public int deleteById(Integer id) {
        return fileDao.deleteById(id);
    }
}
